package com.smarthome.core.propertymanager.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShOrdersPaymentDao;
import com.smarthome.base.service.ShOrdersPaymentService;
import com.smarthome.base.service.ShOrdersService;
import com.smarthome.base.service.ShPropertyFeePaymentService;
import com.smarthome.base.service.ShPropertyFeeService;
import com.smarthome.core.payment.alipay.util.BankCallbackDomain;
import com.smarthome.core.propertymanager.PropertyFeePaymentService;
import com.smarthome.domain.bean.ShOrders;
import com.smarthome.domain.bean.ShOrdersPayment;
import com.smarthome.domain.bean.ShPropertyFee;
import com.smarthome.domain.bean.ShPropertyFeePayment;
import com.smarthome.domain.enums.AppIdEnum;
import com.smarthome.domain.enums.OrdersPaymentStatus;
import com.smarthome.domain.enums.PropertyFeePaymentStatusEnum;
import com.smarthome.domain.enums.PropertyFeeStatusEnum;

@Service("propertyFeePaymentService")
public class PropertyFeePaymentServiceImpl implements PropertyFeePaymentService {

    public static final Logger LOGGER = LoggerFactory.getLogger(PropertyFeePaymentServiceImpl.class);
    @Resource
    private ShPropertyFeePaymentService shPropertyFeePaymentService;
    @Resource
    private ShPropertyFeeService shPropertyFeeService;
    @Resource
    private ShOrdersPaymentService shOrdersPaymentService;
    @Resource
    private ShOrdersService shOrdersService;

    @Override
    public void saveOrEdit(ShPropertyFeePayment payment) {
        shPropertyFeePaymentService.saveOrUpdate(payment);
    }

    @Override
    public List<ShPropertyFeePayment> selectListBetweenMonth(ShPropertyFeePayment payment) {
        return shPropertyFeePaymentService.selectListBetweenMonth(payment);
    }

    @Override
    public void finishPayment(BankCallbackDomain bankCallbackDomain) {
        if (bankCallbackDomain.getAppIdObj() == AppIdEnum.PROPERTY) {
            ShPropertyFeePayment payment = new ShPropertyFeePayment();
            payment.setPaymentSn(String.valueOf(bankCallbackDomain.getOrderId()));
            List<ShPropertyFeePayment> payments = shPropertyFeePaymentService.selectEntryList(payment);
            if (null != payments && payments.size() > 0) {
                payment = payments.get(0);
                String[] feeIds = payment.getFeeIds().split("#");
                for (String feeId : feeIds) {
                    ShPropertyFee fee = new ShPropertyFee();
                    fee.setId(Long.parseLong(feeId));
                    fee.setStatus(PropertyFeeStatusEnum.PAY_YES.getCode());
                    fee.setPaymentSn(String.valueOf(bankCallbackDomain.getOrderId()));
                    shPropertyFeeService.updateById(fee);
                }
                payment.setChannelSn(bankCallbackDomain.getTranSerialNo());
                payment.setStatus(PropertyFeePaymentStatusEnum.SUCCESS.getCode());
                shPropertyFeePaymentService.updateById(payment);
            }
        } else if (bankCallbackDomain.getAppIdObj() == AppIdEnum.ORDERS) {
            ShOrdersPayment payment = new ShOrdersPayment();
            payment.setPaymentSn(String.valueOf(bankCallbackDomain.getOrderId()));
            List<ShOrdersPayment> payments = shOrdersPaymentService.selectEntryList(payment);
            if (null != payments && payments.size() > 0) {
                payment = payments.get(0);
                ShOrders order = new ShOrders();
                order.setId(payment.getOrderId());
                order.setPaymentStatus(OrdersPaymentStatus.ALIPAY.getType());
                shOrdersService.updateById(order);
                payment.setChannelSn(bankCallbackDomain.getTranSerialNo());
                payment.setStatus(PropertyFeePaymentStatusEnum.SUCCESS.getCode());
                shOrdersPaymentService.updateById(payment);
            }

        } else {
            LOGGER.error("alipay-payment-error:" + bankCallbackDomain.getParameters());
        }
    }

}
